package com.atguigu.gulimall.ware.service.impl;

//import com.atguigu.common.constant.WareConstant;
import com.atguigu.common.constant.WareConstant;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.entity.vo.DoneReqVo;
import com.atguigu.gulimall.ware.entity.vo.MergeReqVo;
import com.atguigu.gulimall.ware.entity.vo.item;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.PurchaseService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Resource
    private PurchaseDetailService purchaseDetailService;

    @Resource
    private WareSkuService wareSkuService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    // 合并采购单

    @Override
    public PageUtils getUnclaimedPurchase(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(w -> {
                w.eq("id", key).or().like("assignee_name", key);
            });
        }
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                wrapper.eq("status", 0).or().eq("status", 1)
        );
        return new PageUtils(page);
    }

    // 合并采购需求
    @Transactional
    @Override
    public void mergePurchase(MergeReqVo mergeReqVo) {
        // 获取采购单的id
        Long purchaseId = mergeReqVo.getPurchaseId();
        if(purchaseId == null){
            // 1.新建采购单
            PurchaseEntity purchaseEntity = new PurchaseEntity();

            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }

        // 2.修改采购需求
        List<Long> items = mergeReqVo.getItems();

        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(i -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();

            detailEntity.setId(i);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);

        // 更新采购单的时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

    // 领取采购单
    @Transactional
    @Override
    public void receivePurchase(List<Long> ids) {
        // 1.修改被领取采购单的状态
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            } else {
                return false;
            }
        }).map(item -> {
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());

        // 更新数据库
        this.updateBatchById(collect);

        // 2.修改采购需求状态
        ids.stream().forEach(id -> {
            List<PurchaseDetailEntity> detailEntityList = purchaseDetailService.getBypurchaseId(id);

            List<PurchaseDetailEntity> collect1 = detailEntityList.stream().map(item -> {
                PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.RECEIVE.getCode());
                detailEntity.setId(item.getId());
                return detailEntity;
            }).collect(Collectors.toList());
            // 保存
            purchaseDetailService.updateBatchById(collect1);
        });


    }

    // 完成采购
    @Transactional
    @Override
    public void done(DoneReqVo doneReqVo) {

        // 1.更新采购需求
        List<item> items = doneReqVo.getItems();
        Boolean flag  = true;
        for (item item : items) {
            if(item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = false;
                // 采购失败
                log.error("采购单已被采购，采购失败");
            }else {
                // 采购成功
                // 更新sku的库存信息  `wms_ware_sku`
                PurchaseDetailEntity detailEntity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(detailEntity.getSkuId(), detailEntity.getWareId(), detailEntity.getSkuNum());

            }
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(item.getItemId());
            detailEntity.setStatus(item.getStatus());

            // 保存
            purchaseDetailService.updateById(detailEntity);

        }

        // 2.更新采购单
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(doneReqVo.getId());
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        // 保存
        this.updateById(purchaseEntity);
    }

    /**
     * 完成采购
     *
     * @param doneReqVo
     */
//    @Override
//    public void done(DoneReqVo doneReqVo) {
//        // 1、改变采购项的状态
//        List<item> items = doneReqVo.getItems();
//        Boolean flag = true;
//        List<PurchaseDetailEntity> updates = new ArrayList<>(10);
//        for (item item : items) {
//            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
//            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
//                // 采购失败
//                log.error("采购单已被采购，采购失败");
//                flag = false;
//                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
//            } else {
//                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
//                // 3、将成功采购的入库
//                PurchaseDetailEntity detailEntity = purchaseDetailService.getById(item.getItemId());
//                wareSkuService.addStock(detailEntity.getSkuId(), detailEntity.getWareId(), detailEntity.getSkuNum());
//            }
//            purchaseDetailEntity.setId(item.getItemId());
//            updates.add(purchaseDetailEntity);
//        }
//        purchaseDetailService.updateBatchById(updates);
//        // 2、改变采购状态
//        PurchaseEntity purchaseEntity = baseMapper.selectById(doneReqVo.getId());
//        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
//        purchaseEntity.setId(doneReqVo.getId());
//        baseMapper.updateById(purchaseEntity);
//    }
    /**
     * 合并采购单
     *
     * @param mergeReqVo
     */
//    @Transactional
//    @Override
//    public void mergePurchase(MergeReqVo mergeReqVo) {
//        Long purchaseId = mergeReqVo.getPurchaseId();
//        if (purchaseId == null) {
//            // 新建的采购单
//            PurchaseEntity purchaseEntity = new PurchaseEntity();
//            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATE.getCode());
//            baseMapper.insert(purchaseEntity);
//            purchaseId = purchaseEntity.getId();
//        } else {
//            // 确认采购单状态为0或者1
//            PurchaseEntity purchaseEntity = baseMapper.selectById(purchaseId);
//            if (purchaseEntity.getStatus() != WareConstant.PurchaseStatusEnum.CREATE.getCode() || purchaseEntity.getStatus() != WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
//                log.error("采购单状态不对，无法合并");
//                return;
//            }
//        }
//        List<Long> items = mergeReqVo.getItems();
//        Long finalPurchaseId = purchaseId;
//        List<PurchaseDetailEntity> purchaseDetailEntityList = items.stream().map(item -> {
//            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
//            purchaseDetailEntity.setId(item);
//            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
//            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
//            return purchaseDetailEntity;
//        }).collect(Collectors.toList());
//        purchaseDetailService.saveBatch(purchaseDetailEntityList);
//    }

    /**
     * 领取采购单
     *
     * @param ids
     */
//    @Override
//    public void receivePurchase(List<Long> ids) {
//        // 查询未分配或新建的采购单
//        List<PurchaseEntity> purchaseEntityList = ids.stream().map(id -> {
//            PurchaseEntity purchaseEntity = baseMapper.selectById(id);
//            return purchaseEntity;
//        }).filter(item -> {
//            return (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATE.getCode() || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode());
//        }).map(item -> {
//            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
//            return item;
//        }).collect(Collectors.toList());
//        // 修改
//        this.updateBatchById(purchaseEntityList);
//        // 改变采购项状态
//        purchaseEntityList.forEach(item -> {
//            List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailService.getBypurchaseId(item.getId());
//            List<PurchaseDetailEntity> detailEntityList = purchaseDetailEntities.stream().map(entity -> {
//                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
//                purchaseDetailEntity.setId(entity.getId());
//                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
//                return purchaseDetailEntity;
//            }).collect(Collectors.toList());
//            purchaseDetailService.updateBatchById(detailEntityList);
//        });
//    }


}